//
//  CustomCalloutView.m
//  DingDongXiChe
//
//  Created by wtjr on 2018/1/5.
//  Copyright © 2018年 xieyingze. All rights reserved.
//

#import "CustomCalloutView.h"

#define kArrorHeight        10

@interface CustomCalloutView()

@property (nonatomic, strong) UILabel *subtitleLabel;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation CustomCalloutView


#pragma mark - BaseMethod

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews {
    // 添加图片，即商户图
    self.portraitView = [UIImageView new];
    self.portraitView.backgroundColor = kCOLOR_lightGray;
    [self addSubview:self.portraitView];
    [self.portraitView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@5);
        make.top.equalTo(@5);
        make.width.equalTo(@70);
        make.height.equalTo(@50);
    }];
    
    
    // 添加标题，即商户名
    self.titleLabel = [UILabel new];
    self.titleLabel.textAlignment = NSTextAlignmentLeft;
    [self.titleLabel setLabelText:@"subtitle" textColor:KCOLOR_font_normal font:kFONT_Bold(12)];
    [self addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.portraitView.mas_right).offset(5);
        make.right.equalTo(@-5);
        make.top.equalTo(self.portraitView);
        make.height.equalTo(@20);
    }];
    
    // 添加副标题，即商户地址
    self.subtitleLabel = [UILabel new];
    self.subtitleLabel.textAlignment = NSTextAlignmentLeft;
    [self.subtitleLabel setLabelText:@"subtitle" textColor:KCOLOR_font_light font:kFONT_mainFont(10)];
    [self addSubview:self.subtitleLabel];
    [self.subtitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel);
        make.right.equalTo(self.titleLabel);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(5);
        make.height.equalTo(@15);
    }];
}

#pragma mark - Interface


#pragma mark - Private

- (void)drawRect:(CGRect)rect {
    [self drawInContext:UIGraphicsGetCurrentContext()];
    self.layer.shadowColor = kCOLOR_shadowDark.CGColor;
    self.layer.shadowOpacity = 1.0;
    self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
}

- (void)drawInContext:(CGContextRef)context {
    CGContextSetLineWidth(context, 2.0);
    CGContextSetFillColorWithColor(context, kCOLOR_white.CGColor);
    [self getDrawPath:context];
    CGContextFillPath(context);
}

- (void)getDrawPath:(CGContextRef)context {
    CGRect rrect = self.bounds;
    CGFloat radius = 5.0;
    CGFloat minx = CGRectGetMinX(rrect),
    midx = CGRectGetMidX(rrect),
    maxx = CGRectGetMaxX(rrect);
    CGFloat miny = CGRectGetMinY(rrect),
    maxy = CGRectGetMaxY(rrect)-kArrorHeight;
    
    CGContextMoveToPoint(context, midx+kArrorHeight, maxy);
    CGContextAddLineToPoint(context,midx, maxy+kArrorHeight);
    CGContextAddLineToPoint(context,midx-kArrorHeight, maxy);
    
    CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);
    CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, maxx, radius);
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
    CGContextClosePath(context);
}

#pragma mark - Delegate


#pragma mark - TouchEvent


#pragma mark - Setter

- (void)setTitle:(NSString *)title {
    self.titleLabel.text = title;
}

- (void)setSubtitle:(NSString *)subtitle {
    self.subtitleLabel.text = subtitle;
}

- (void)setImage:(UIImage *)image {
    self.portraitView.image = image;
}


#pragma mark - Getter

@end
